<?
$mb_id = $_GET['mb_id'];
$apikey = $_GET['apikey'];
$g4_path = "../..";

include_once("$g4_path/common.php");
include_once("$g4[path]/lib/finalpaper.lib.php");

/**
 Google Chrome Extension v0.36+
 통합 버전: newComments + newMessages,  profile,  newPosts
 */
header ("Content-Type:text/xml"); 
if(!$mb_id || !$apikey) { 
	echo "<error>NO_PARAM</error>"; 
	exit;
}

$sql = " SELECT apikey FROM fpn_mb_apikey WHERE mb_id='{$mb_id}' ";
$res = mysql_query($sql);
if(!mysql_num_rows($res)) {	// no apikey
	echo "<error>NO_APIKEY</error>";
	exit;
}
$row = mysql_fetch_array($res);
if($row[apikey]!=$apikey) { 
	echo "<error>WRONG_APIKEY</error>";
	exit;
}

// v0.35+  profile info
if($_GET['profile']) { // for testing : copied from bbs/view_profile.php
	echo "<profile>";
	$member = get_member($mb_id);
	$mb_dir = substr($member[mb_id],0,2);
	// 별명
	echo "<nick>".$member['mb_nick']."</nick>";
	// 대표그림
	echo "<picture>".urlencode("http://www.finalpaper.net/data/member/$mb_dir/{$member[mb_id]}.jpg")."</picture>";
	// 직급
	if($member['mb_level']>=2&&$member['mb_level']<=4) $position = "학생";	// JJ 2010/07, no level 4 though
	else if($member['mb_level']==5) $position = "교직원";
	else if($member['mb_level']==6) $position = "모델러";
	else if($member['mb_level']==7) $position = "디자이너";
	else if($member['mb_level']==8) $position = "우수고객";
	else if($member['mb_level']==9) $position = "교감";
	else if($member['mb_level']==10) $position = "교장";
	else $position = "방문자";
	if($member['mb_id']=='gssb56') $position = "누리마당 운영자";
	echo "<position>".$position."</position>";
	// 호봉
	$hb=date("Y")-substr($member['mb_datetime'],0,4)+1; // JJ 2010/08/15
	echo "<years>".$hb."</years>";

	// 뺏지
	$etcinfo=explode("|",$member[mb_6]);
	$badge = "";
	if ($etcinfo[0]==1) 
		$badge = "http://www.finalpaper.net/fpnbadge.jpg";
	echo "<badge>".urlencode($badge)."</badge>";
	
	// 마크
	echo "<mark>".urlencode("http://www.finalpaper.net/data/member/$mb_dir/{$member[mb_id]}.gif")."</mark>";

	// 경험치
	echo "<exp>".$member['mb_xp']."</exp>";

	// 랭킹
	echo "<rank>".$member['mb_rank']."</rank>";

	// 소개글
	echo "<signature>".$member['mb_signature']."</signature>";

	// 포인트
	echo "<point>".$member['mb_point']."</point>";
	echo "<point_c>".$member['mb_point_c']."</point_c>";
	echo "<point_m>".$member['mb_point_m']."</point_m>";
	echo "<point_y>".$member['mb_point_y']."</point_y>";
	echo "<point_p>".$member['mb_point_p']."</point_p>";
	echo "<recomended>".$member['mb_good']."</recomended>";

	// 관리자 게시판
	if($member[mb_level]>=5) {
		$boards = array();
		$mb_id = $member[mb_id];
		$res = sql_query(" select bo_table, bo_subject from {$g4[board_table]} where bo_admin REGEXP '^{$mb_id}$|^{$mb_id},|,{$mb_id},|,{$mb_id}$' ");
		for ($i=0; $row=sql_fetch_array($res); $i++) {
			array_push($boards, $row[bo_table]."(".$row[bo_subject].")");
		}
		echo "<watchBoards>".implode(",",$boards)."</watchBoards>";
	}
	
	echo "</profile>";

	return;
}

// v0.36+  newComments + newMessages
/*
 DOM structure:
	myPage
		newMessages
			total
			message
				name
				content
				URL
				datetime
		newComments
			total
			comment
				group
				board
				title
				URL
				name
				datetime
*/
echo "<myPage>";
// unread messages
$sql = " SELECT me.*, mb.mb_nick 
	FROM $g4[memo_table] me LEFT JOIN $g4[member_table] mb ON (me.me_send_mb_id = mb.mb_id)
	WHERE me_recv_mb_id = '$mb_id' AND me_read_datetime = '0000-00-00 00:00:00' 
	ORDER BY me_id DESC ";
$result = sql_query($sql);
echo "<newMessages>";
echo "<total>".mysql_num_rows($result)."</total>";
for ($i=0; $row=sql_fetch_array($result); $i++)	{
	echo "<message>{$i}";
	echo "<name>";
	if ($row[mb_nick]) echo $row[mb_nick];
	else echo "정보없음";
	echo "</name>";
	echo "<content>".fp_cutstr($row[me_memo],25,'…')."</content>";
	echo "<URL>".urlencode("http://www.finalpaper.net/bbs/memo_view.php?me_id=$row[me_id]&kind=recv")."</URL>";
	echo "<datetime>".substr($row[me_send_datetime],2,14),"</datetime>";
	echo "</message>";
}
echo "</newMessages>";

// unckecked comments on my post/comment											
$sql = " SELECT * FROM  `fpn_my_check` WHERE  `mb_to`='$mb_id' AND `is_check` = 0";
$result = sql_query($sql);
echo "<newComments>";
echo "<total>".mysql_num_rows($result)."</total>";
for ($i=0; $row = sql_fetch_array($result); $i++) {
	$gr_bo = sql_fetch(" SELECT gr_subject, bo_subject FROM g4_board b, g4_group g WHERE b.gr_id=g.gr_id AND b.bo_table='{$row[bo_table]}' ");
	$member = sql_fetch(" SELECT mb_nick FROM g4_member WHERE mb_id='{$row[mb_from]}' ");
	echo "<comment>{$i}";
	echo "<group>{$gr_bo[gr_subject]}</group><board>{$gr_bo[bo_subject]}</board>";
	echo "<title>".htmlspecialchars(fp_cutstr($row[wr_content],25,'…'))."</title>";
	echo "<URL>".urlencode("http://www.finalpaper.net/bbs/board.php?bo_table={$row[bo_table]}&wr_id={$row[wr_id]}&chk=1&check_comment_id={$row[wr_comment_id]}#c_{$row[wr_comment_id]}")."</URL>";
	echo "<name>{$member[mb_nick]}</name>";
	echo "<datetime>".substr($row[wr_datetime],5,11)."</datetime>";
	echo "</comment>";
}
echo "</newComments>";
echo "</myPage>";
?>